<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div style="padding: 20px">
          <!-- 页头 -->
          <el-row>
            <el-form
              :inline="true"
              :model="formInline"
              class="demo-form-inline"
            >
              <el-col :xs="24" :sm="24" :md="10" :lg="8" :xl="8">
                <el-form-item label="标签名称" style="margin-left: 0.2rem">
                  <el-input v-model="formInline.user"></el-input> </el-form-item
              ></el-col>
              <el-col :xs="24" :sm="24" :md="10" :lg="12" :xl="12">
                <el-form-item label="状态">
                  <el-select v-model="formInline.region" placeholder="请选择">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
                <el-button @click="del">清除</el-button>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
              </el-col>
            </el-form>
            <el-col :xs="6" :sm="6" :md="4" :lg="4" :xl="4" align="right">
              <el-button type="success" icon="el-icon-edit" @click="add"
                >新增标签</el-button
              >
            </el-col>
          </el-row>
          <!-- 页头 -->
          <!-- 消息文案 -->
          <el-alert
            :title="`数据一共${this.page.total}条`"
            type="info"
            show-icon
            :closable="false"
          />
          <!-- 消息文案 -->
          <!-- 表格 -->
          <el-table :data="list" style="width: 100%; margin-top: 0.25rem">
            <el-table-column type="index" label="序号" width="80" />
            <el-table-column prop="subjectName" label="所属学科" />
            <el-table-column prop="tagName" label="标签名称" />
            <el-table-column prop="username" label="创建者" />
            <el-table-column label="创造日期">
              <template slot-scope="scope">
                <!-- 调用全局过滤方法 -->
                <span>{{ scope.row.addDate | parseTimeByString }}</span>
              </template>
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <span>{{ scope.row.state === 1 ? "已启用" : "已禁用" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="medium"
                  type="text"
                  @click="disabled(scope.row)"
                  >{{ scope.row.state === 1 ? "禁用" : "启用" }}</el-button
                >
                <el-button
                  size="medium"
                  type="text"
                  :disabled="scope.row.state === 0"
                  @click="add1(scope.row)"
                  >修改</el-button
                >
                <el-button
                  size="medium"
                  type="text"
                  :disabled="scope.row.state === 0"
                  @click="open(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- 表格 -->
          <!-- 分页器 -->
          <div class="block" style="margin-top: 0.25rem; text-align: right">
            <el-pagination
              @current-change="handleCurrentChange"
              @size-change="sizeChange"
              :current-page="page.page"
              :page-size="page.pagesize"
              background
              :total="page.total"
              layout=" prev, pager,next, sizes, jumper"
              :page-sizes="[5, 10, 20, 50]"
            />
          </div>
          <!-- 分页器组件 -->
          <NewDirectorys
            :Show="Show"
            :is-show.sync="isShow"
            @delDepts="directoryPage"
            :NewList="OldList"
            :ChanGen="ChanGen"
            ref="addDept"
            :list="NewList"
          />
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import NewDirectorys from '../components/tags-add.vue'
import { listOK, changeStateOk, remove, Simple } from '@/api/hmmm/tags.js'
export default {
  name: 'Directory',
  components: {
    NewDirectorys
  },
  data () {
    return {
      Show: false,
      // 修改和新增切换
      ChanGen: 0,
      // 弹框开关
      isShow: false,
      formInline: {
        user: '',
        region: ''
      },
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      // 存状态
      state: '',
      // id
      OldList: {},
      NewList: []
    }
  },
  computed: {},
  // 页面加载是触发，获取数据
  created () {
    // 所有的列表 ajax
    this.directoryPage()
    // 所有的标签列表数据
    this.getList()
    // this.tagName()
  },
  methods: {
    // 所有的标签
    async getList () {
      try {
        const { data } = await Simple()
        this.NewList = data
      } catch (err) {
        console.log(err)
      }
    },
    // 删除
    open (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: id })
          this.directoryPage()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 获取状态
    async disabled (date) {
      if (date.state === 0) {
        this.state = 1
      } else {
        this.state = 0
      }
      const params = {
        state: this.state,
        id: date.id
      }
      // 调用标签方法
      await changeStateOk(params)
      // 判断标签名称和状态是否为空
      if (this.formInline.region === '' && this.formInline.user === '') {
        await this.directoryPage()
      } else {
        // 执行搜索
        this.onSubmit()
      }
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    },
    // 新增按钮
    add () {
      this.isShow = true
      this.ChanGen = 0
      this.formInline.region = ''
      this.formInline.user = ''
    },
    // 修改按钮
    add1 (data) {
      this.isShow = true
      this.ChanGen = 1
      this.OldList = data
      this.$refs.addDept._data.ruleForm.tagName = data.tagName
      this.$refs.addDept._data.ruleForm.subjectName = data.subjectID
    },
    // 清空标签名称和状态
    async del () {
      if (this.formInline.region !== '' || this.formInline.user !== '') {
        this.formInline.region = ''
        this.formInline.user = ''
        // console.log(this.list)
      }
    },
    // 根据标签名称和状态进行搜索
    async onSubmit () {
      if (this.formInline.region !== '' || this.formInline.user !== '') {
        await this.tagName()
      } else if (this.formInline.region === '' && this.formInline.user === '') {
        await this.directoryPage()
      }
    },
    // 默认获取所有的列表 ajax
    async directoryPage () {
      if (
        this.$route.query.id !== undefined &&
        this.$route.query.name !== undefined
      ) {
        this.Show = true
        const id = this.$route.query.id
        // const tagName = JSON.stringify(this.$route.query.name)
        try {
          const { data } = await listOK({ ...this.page, subjectID: id })
          this.list = data.items
          this.page.total = data.counts
        } catch (err) {
          console.log(err)
        }
      } else {
        try {
          const { data } = await listOK(this.page)
          this.list = data.items
          this.page.total = data.counts
        } catch (err) {
          console.log(err)
        }
      }
    },
    // 根据标签名称 和 状态 和所有的列表 ajax (传入名称和状态)
    async tagName () {
      if (
        this.$route.query.id !== undefined &&
        this.$route.query.name !== undefined
      ) {
        // this.formInline.region === '' ? '' : this.formInline.region
        if (this.formInline.region) {
          const { data } = await listOK({
            ...this.page,
            subjectID: this.$route.query.id,
            state: +this.formInline.region,
            tagName: this.formInline.user
          })
          this.list = data.items
          this.page.total = data.counts
        } else {
          const { data } = await listOK({
            ...this.page,
            subjectID: this.$route.query.id,
            tagName: this.formInline.user
          })
          this.list = data.items
          this.page.total = data.counts
        }
      } else {
        // 4种情况 1.有名称,有状态 2.有状态,没有名称 3.有名称.没有状态 4.报错
        try {
          if (this.formInline.user !== '' && this.formInline.region !== '') {
            const { data } = await listOK({
              ...this.page,
              tagName: this.formInline.user,
              state: +this.formInline.region
            })
            this.list = data.items
            this.page.total = data.counts
          } else if (this.formInline.region !== '') {
            const { data } = await listOK({
              ...this.page,
              state: +this.formInline.region
            })
            this.list = data.items
            this.page.total = data.counts
          } else if (this.formInline.user !== '') {
            const { data } = await listOK({
              ...this.page,
              tagName: this.formInline.user
            })
            this.list = data.items
            this.page.total = data.counts
          }
        } catch (err) {
          console.log(err)
        }
      }
    },
    // 分页器
    handleCurrentChange (val) {
      this.page.page = val
      if (this.formInline.region !== '') {
        this.tagName()
      } else {
        this.directoryPage()
      }
    },
    sizeChange (val) {
      this.page.pagesize = val
      if (this.formInline.region !== '') {
        this.tagName()
      } else {
        this.directoryPage()
      }
    },
    subject () {
      this.$router.push({ path: 'list' })
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  background-color: #fff;
  margin: 0.225rem 0.175rem;
}
</style>
